import json


class RedirectType:

    def __init__(self):
        self.click_enable = False
        self.update_enable = False
        self.online_enable = False
        self.watched_enable = False
        self.like_info = {
            'uname': None,
            'like_text': None,
            'click_count': None
        }
        self.online = {}

    #  type1:弹幕消息
    @classmethod
    def DANMU_MSG(self, jd):
        #  判断是哪一类  弹幕消息、关注主播
        print(jd)
        # data = jd['info']
        # Id = data[2][0]
        # Nickname = data[2][1]
        # Content = data[1]
        # new_message = {
        #     'User': {
        #         'Id': Id,
        #         'Nickname': Nickname
        #     },
        #     'Content': Content
        # }
        # new_message = {
        #     'Type': 1,
        #     'Data': str(new_message).replace("'", r'"')
        # }
        # # new_message = json.dumps(new_message, ensure_ascii=False)
        # print(new_message)

    #  type2:直播间点赞量
    @classmethod
    def LIKE_INFO_V3(self, jd):
        if jd['cmd'] == 'LIKE_INFO_V3_CLICK':
            uname = jd['data']['uname']
            like_text = jd['data']['like_text']
            self.click_enable = True
            self.like_info['uname'] = uname
            self.like_info['like_text'] = like_text
        if jd['cmd'] == 'LIKE_INFO_V3_UPDATE':
            click_count = jd['data']['click_count']
            self.update_enable = True
            self.like_info['click_count'] = click_count
        if self.click_enable and self.update_enable:
            new_message = {
                'User': {
                    'Nickname': self.LIKE_INFO['uname']
                },
                'Content': self.like_info['uname'] + " " + self.like_info['like_text'] + "总点赞" + self.like_info['click_count']
            }
            new_message = {
                'Type': 2,
                'Data': str(new_message).replace("'", r'"')
            }
            new_message = json.dumps(new_message, ensure_ascii=False)
            print(new_message)
            self.click_enable = False
            self.update_enable = False
            self.like_info = {
                'uname': None,
                'like_text': None,
                'click_count': None
            }

    #  type3：进入直播间
    @classmethod
    def INTERACT_WORD(cls, jd):
        # jd = {'cmd': 'INTERACT_WORD', 'data': {'contribution': {'grade': 0}, 'core_user_type': 0, 'dmscore': 12, 'fans_medal': {'anchor_roomid': 1589452, 'guard_level': 0, 'icon_id': 0, 'is_lighted': 0, 'medal_color': 12478086, 'medal_color_border': 12632256, 'medal_color_end': 12632256, 'medal_color_start': 12632256, 'medal_level': 14, 'medal_name': '纯爷萌', 'score': 53950, 'special': '', 'target_id': 3570093}, 'identities': [1], 'is_spread': 0, 'msg_type': 1, 'privilege_type': 0, 'roomid': 25971921, 'score': 1686021184065, 'spread_desc': '', 'spread_info': '', 'tail_icon': 0, 'timestamp': 1686021184, 'trigger_time': 1686021182977866500, 'uid': 285199088, 'uname': 'william19999', 'uname_color': ''}}
        uname = jd['data']['uname']
        roomid = jd['data']['roomid']
        uid = jd['data']['uid']
        new_message = {
            'User': {
                'Id': uid,
                'Nickname': uname
            },
            'Content': uname + ' 来了 直播间',
            'RoomId': roomid
        }
        new_message = {
            'Type': 3,
            'Data': str(new_message).replace("'", r'"')
        }
        new_message = json.dumps(new_message, ensure_ascii=False)
        print(new_message)

    #  type5: 送礼物
    @classmethod
    def SEND_GIFT(cls, jd):
        # jd = {'cmd': 'SEND_GIFT', 'data': {'action': '投喂', 'bag_gift': None, 'batch_combo_id': 'batch:gift:combo_id:590398997:6639802:31164:1686040573.5108', 'batch_combo_send': {'action': '投喂', 'batch_combo_id': 'batch:gift:combo_id:590398997:6639802:31164:1686040573.5108', 'batch_combo_num': 1, 'blind_gift': None, 'gift_id': 31164, 'gift_name': '粉丝团灯牌', 'gift_num': 1, 'send_master': None, 'uid': 590398997, 'uname': '摸鱼千岁'}, 'beatId': '', 'biz_source': 'live', 'blind_gift': None, 'broadcast_id': 0, 'coin_type': 'gold', 'combo_resources_id': 1, 'combo_send': {'action': '投喂', 'combo_id': 'gift:combo_id:590398997:6639802:31164:1686040573.5097', 'combo_num': 1, 'gift_id': 31164, 'gift_name': '粉丝团灯牌', 'gift_num': 1, 'send_master': None, 'uid': 590398997, 'uname': '摸鱼千岁'}, 'combo_stay_time': 5, 'combo_total_coin': 1000, 'crit_prob': 0, 'demarcation': 2, 'discount_price': 1000, 'dmscore': 24, 'draw': 0, 'effect': 3, 'effect_block': 0, 'face': 'https://i2.hdslb.com/bfs/face/0fabd901a2bfa48cfa7a57c9093c7d58f2629852.jpg', 'face_effect_id': 0, 'face_effect_type': 0, 'float_sc_resource_id': 0, 'giftId': 31164, 'giftName': '粉丝团灯牌', 'giftType': 0, 'gold': 0, 'guard_level': 0, 'is_first': True, 'is_join_receiver': False, 'is_naming': False, 'is_special_batch': 0, 'magnification': 1, 'medal_info': {'anchor_roomid': 0, 'anchor_uname': '', 'guard_level': 0, 'icon_id': 0, 'is_lighted': 1, 'medal_color': 6126494, 'medal_color_border': 6126494, 'medal_color_end': 6126494, 'medal_color_start': 6126494, 'medal_level': 5, 'medal_name': '仙鸽鸽', 'special': '', 'target_id': 25851315}, 'name_color': '', 'num': 1, 'original_gift_name': '', 'price': 1000, 'rcost': 301252775, 'receive_user_info': {'uid': 6639802, 'uname': '陈哥1'}, 'remain': 0, 'rnd': '2191725465369120768', 'send_master': None, 'silver': 0, 'super': 0, 'super_batch_gift_num': 1, 'super_gift_num': 1, 'svga_block': 0, 'switch': True, 'tag_image': '', 'tid': '2191725465369120768', 'timestamp': 1686040573, 'top_list': None, 'total_coin': 1000, 'uid': 590398997, 'uname': '摸鱼千岁'}}
        data = jd['data']['combo_send']
        uname = data['uname']
        uid = data['uid']
        gift_name = data['gift_name']
        gift_num = data['gift_num']
        new_message = {
            'User': {
                'Id': uid,
                'Nickname': uname
            },
            'Content': uname + ":送给主播 " + str(gift_num) + "个" + gift_name
        }
        new_message = {
            'Type': 5,
            'Data': str(new_message).replace("'", r'"')
        }
        new_message = json.dumps(new_message, ensure_ascii=False)
        print(new_message)

    #  type6，直播间在线人数
    @classmethod
    def ONLINE_RANK_COUNT(self, jd):
        if jd['cmd'] == 'ONLINE_RANK_COUNT':
            # jd = {'cmd': 'ONLINE_RANK_COUNT', 'data': {'count': 1478}}
            count = jd['data']['count']
            self.online['count'] = count
            self.online_enable = True
        if jd['cmd'] == 'WATCHED_CHANGE':
            # jd = {'cmd': 'WATCHED_CHANGE', 'data': {'num': 14641, 'text_small': '1.4万', 'text_large': '1.4万人看过'}}
            total_count = jd['data']['text_small']
            self.online['total_count'] = total_count
            self.watched_enable = True
        if self.watched_enable and self.online_enable:
            new_message = {
                "OnlineUserCount": self.online['count'],
                "TotalUserCountStr": self.online['total_count']

            }
            new_message = {
                'Type': 6,
                'Data': str(new_message).replace("'", r'"')
            }
            new_message = json.dumps(new_message, ensure_ascii=False)
            print(new_message)
            self.watched_enable = False
            self.online_enable = False
            self.online = {}